package com.jgdt.jgdtkasannong_university_gene_backend.service.content.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jgdt.jgdtkasannong_university_gene_backend.common.response.PageRequest;
import com.jgdt.jgdtkasannong_university_gene_backend.dto.ContenMediaDto;
import com.jgdt.jgdtkasannong_university_gene_backend.entity.content.MenuContent;
import com.jgdt.jgdtkasannong_university_gene_backend.entity.content.SysMenu;
import com.jgdt.jgdtkasannong_university_gene_backend.mapper.content.ContentGeneralMapper;
import com.jgdt.jgdtkasannong_university_gene_backend.mapper.content.MenuContentMapper;
import com.jgdt.jgdtkasannong_university_gene_backend.service.content.ContentGeneralService;
import com.jgdt.jgdtkasannong_university_gene_backend.service.content.MediaService;
import com.jgdt.jgdtkasannong_university_gene_backend.service.content.MenuContentService;
import com.jgdt.jgdtkasannong_university_gene_backend.service.sys.SysValueSetService;
import com.jgdt.jgdtkasannong_university_gene_backend.utils.BeanUtils;
import com.jgdt.jgdtkasannong_university_gene_backend.vo.content.menu_content.ContentGeneralVo;
import com.jgdt.jgdtkasannong_university_gene_backend.vo.content.menu_content.SysMenuBaseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单内容关联表 服务实现类
 * </p>
 *
 * @author chenzg
 * @since 2025-04-24
 */
@Service
public class MenuContentServiceImpl  extends ServiceImpl<MenuContentMapper,MenuContent> implements MenuContentService{

    @Autowired
    private ContentGeneralService contentGeneralService;
    @Autowired
    private MenuContentMapper menuContentMapper;
    @Autowired
    private MediaService mediaService;
    @Autowired
    private SysValueSetService sysValueSetService;
    @Autowired
    private ContentGeneralMapper generalMapper;

    @Override
    public List<SysMenuBaseVo> contenShowByMenuId(List<SysMenu> sysMenuList) {
        List<SysMenuBaseVo> sysMenuBaseVos = new ArrayList<>();
        List<Integer> menuIds = sysMenuList.stream().map(SysMenu::getId).collect(Collectors.toList());
        Map<Integer, SysMenu> sysMenuListMap = sysMenuList.stream().collect(Collectors.toMap(SysMenu::getId, Function.identity()));
        //查询 所有相关的 内容、图片相关数据
        List<ContenMediaDto> contenMediaDtoList = contentGeneralService.contenMediaByMenuIds(menuIds);
        Map<Integer, List<ContenMediaDto>> contenMediaDtoListMap = contenMediaDtoList.stream().collect(Collectors.groupingBy(ContenMediaDto::getMenuId));
        //组装Vo
        for (Integer key : contenMediaDtoListMap.keySet()) {
            SysMenuBaseVo sysMenuBaseVo = new SysMenuBaseVo();
            SysMenu sysMenu = sysMenuListMap.get(key);
            BeanUtil.copyProperties(sysMenu, sysMenuBaseVo);
            List<ContentGeneralVo> contentGeneralVoList = new ArrayList<>();
            List<ContenMediaDto> contenMediaDto = contenMediaDtoListMap.get(key);
            contentGeneralVoList = BeanUtils.convertList(contenMediaDto, ContentGeneralVo.class);
            for (ContentGeneralVo contentGeneralVo:contentGeneralVoList){
                //frontImageUrl 赋值
                contentGeneralVo.setFrontImageUrl(mediaService.getImgUrl(contentGeneralVo.getImageUrl()));
            }
            sysMenuBaseVo.setContentGeneralVos(contentGeneralVoList);
            sysMenuBaseVos.add(sysMenuBaseVo);
        }
        return sysMenuBaseVos;
    }

    @Override
    public IPage<ContentGeneralVo> contenShowPage(PageRequest<ContentGeneralVo> page,Integer menuId) {
        IPage<ContentGeneralVo> sysMenuBaseVos =new Page<>(page.getPageNum(),page.getPageSize());

        PageRequest<ContenMediaDto> pageRequest=new PageRequest<>();
        pageRequest.setPageNum(page.getPageNum());
        pageRequest.setPageSize(page.getPageSize());
        ContenMediaDto contenMediaDto=new ContenMediaDto();
        contenMediaDto.setMediaId(menuId);
        pageRequest.setCondition(contenMediaDto);
        //查询 所有相关的 内容、图片相关数据
        IPage<ContenMediaDto> contenMediaDtoPage = contentGeneralService.contenMediaPage(pageRequest);
        //组装
        List<ContenMediaDto> contenMediaDtoList= contenMediaDtoPage.getRecords();
        List<ContentGeneralVo> sysMenuBaseVoList=BeanUtils.convertList(contenMediaDtoList,ContentGeneralVo.class);
        //frontImageUrl 赋值
        for(ContentGeneralVo vo:sysMenuBaseVoList){
            vo.setFrontImageUrl(mediaService.getImgUrl(vo.getImageUrl()));
        }
        sysMenuBaseVos.setTotal(contenMediaDtoPage.getTotal());
        sysMenuBaseVos.setSize(contenMediaDtoPage.getSize());
        sysMenuBaseVos.setRecords(sysMenuBaseVoList);
        return sysMenuBaseVos;
    }

    @Override
    public List<MenuContent> queryByMenuIds(List<Integer> menuIds) {
        if(CollectionUtil.isEmpty(menuIds)){
            return new ArrayList<>();
        }
        LambdaQueryWrapper<MenuContent> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.in(MenuContent::getMenuId,menuIds);
        return menuContentMapper.selectList(queryWrapper);
    }

    @Override
    public void saveUpdataById(List<MenuContent> menuContents) {
        saveOrUpdateBatch(menuContents);
    }

    @Override
    public void deleteByContentId(List<Integer> deleteContentGeneralIds) {
        LambdaQueryWrapper<MenuContent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(MenuContent::getContentId, deleteContentGeneralIds);
        List<MenuContent> menuContents = menuContentMapper.selectList(queryWrapper);
        if (CollectionUtil.isNotEmpty(menuContents)) {
            menuContentMapper.deleteBatchIds(menuContents.stream().map(MenuContent::getId).collect(Collectors.toList()));
            generalMapper.deleteBatchIds(deleteContentGeneralIds);
            //图片相关
            mediaService.deleteByGeneralIds(deleteContentGeneralIds);
        }
    }
}
